<template>
  <div class="box_contant">
    <div class="seach_box">
      <el-input v-model="seachdata" placeholder="请输入商品名称">
        <template #append>
          <el-button @click="seachgood">搜索</el-button>
        </template>
      </el-input>
      <el-button type="success">》》高级搜索</el-button>
    </div>
    <div class="page_contant">
      <div class="title_size">所属类目：</div>
      <el-tabs v-model="activeName">
        <el-tab-pane label="全部" name="all"><classi-fication :goodsdetails="childrendata" :parentsdata="parentsdata" @updatadata="updatadata" /></el-tab-pane>
        <el-tab-pane v-for="v in commodity" :key="v.id" :label="v.name" :name="v.name">
          <classi-fication :goodsdetails="v.children" :parentsdata="parentsdata" @updatadata="updatadata" />
        </el-tab-pane>
        <!-- <el-tab-pane label="角色管理" name="third">角色管理</el-tab-pane>
        <el-tab-pane label="定时任务补偿" name="fourth">定时任务补偿</el-tab-pane> -->
      </el-tabs>
    </div>
  </div>

</template>

<script>
import { getCommondityLists } from '@/api/commodity.js';
import ClassiFication from './ClassiFication.vue';
import { tranListToTreeData } from '@/utils/index.js';
export default {
  components: {
    ClassiFication,
  },
  data() {
    return {
      activeName: 'all',
      commodity: [],
      childrendata: [],
      seachdata: '',
      parentsdata: [],
    };
  },
  created() {
    this.getgoodlist();
  },
  methods: {
    seachgood() {
      if (this.seachdata !== '') {
        const reg = new RegExp(this.seachdata);
        this.childrendata = this.childrendata.filter(v => reg.test(v.name));
        this.activeName = 'all';
      } else {
        this.getgoodlist();
      }
    },
    async  getgoodlist() {
      const res = await getCommondityLists();
      this.commodity = tranListToTreeData(res.data, '');
      this.childrendata = res.data.filter(v => v.pid !== '');
      this.parentsdata = res.data.filter(v => v.pid === '');
    },
    updatadata() {
      this.getgoodlist();
    },
  },
};
</script>

<style  lang="scss">
.box_contant{
  .page_contant{
    display: flex;
    padding: 15px 15px;
      .title_size{
        width: 100px;
        font-size: 14px;
        height: 40px;
        padding-top: 11px;
        margin-right: 10px;
      }
  }
  .seach_box{
    margin: 30px 0;
    display: flex;
    justify-content: center;
    .el-input{
      margin-right: 5px;
      width: 500px;
      .el-input-group__append{
        background-color: #1890FF;
        .el-button{
          color: #fff;
        }
      }
    }
  }
}
</style>
